from PyQt4 import QtCore
import sys,os,inspect
sys.path.insert(0,os.path.abspath(os.path.join(os.getcwd(), os.pardir)))## add root path 
from KDataBase.KDBManager import KDBManager
from KDataBase import KBuildDB,KQueryDB,KDBUtility
from KDataBase.KUpdateDB import KUpdateDB
from KDataBase.KQueryDB import KQueryDB
from KModel.KSingleStock import KSingleStock
from subprocess import check_output
class KSymbolListGenerator(QtCore.QObject):


	def __init__(self):
		super(KSymbolListGenerator, self).__init__()
		self.rootinpath=r'D:\Dropbox\Trading2013\KunTrading\ConfigFiles\OrigSymbolList'
		self.rootoutpath=r'D:\Dropbox\Trading2013\KunTrading\ConfigFiles\KSymbolList'
	def GenerateListFile(self,filename,country):
		# generate the symbol list of internal format
		pathname=self.rootinpath+r'\\'+filename
		writepath=self.rootoutpath+r'\\'+filename
		data=open(pathname)
		writefileobj=open(writepath,'w')
		orgcount=0
		writtencount=0
		for each_symbol in data:
			if each_symbol:
				fullname=KQueryDB.QueryStockFullName(each_symbol.strip(),'US')
				if fullname:					
					writefileobj.writelines("%s\n" %(fullname))
					writtencount+=1
				else:					
					print('missing # %s' %each_symbol.strip())
				orgcount+=1
		data.close()
		writefileobj.close()
		print("Orig # %d, Found Count # %d" %(orgcount,writtencount))



if __name__ == "__main__":
	#app = QtCore.QCoreApplication(sys.argv)
	console = KSymbolListGenerator()


	console.GenerateListFile('S&P500.txt','US')

	#console.RunTest()
	#sys.exit(app.exec_())